<?php
require 'facebook-php-sdk/src/facebook.php';

$facebook = new Facebook(array(
            'appId' => '171726412964671',
            'secret' => '0ff931e6b5bab347a178de0b3a78fc4a',
        ));

// See if there is a user from a cookie
$user = $facebook->getUser();

if ($user) {
    try {
        // Proceed knowing you have a logged in user who's authenticated.
        $user_profile = $facebook->api('/me');
    } catch (FacebookApiException $e) {
        echo '<pre>' . htmlspecialchars(print_r($e, true)) . '</pre>';
        $user = null;
    }
}

$comparison_id = $_GET["id"];
?>
<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <?php include 'includes.php'; ?> 
        <link href="assets/css/comparison.css" rel="stylesheet" type="text/css">
    </head>
    <body>
        <?php include 'header.php'; ?>
        <div class="container" style="padding-top: 50px; ">
            <?php
            if ($comparison_id) {
                print "<p> Showing comparison Id: " . $comparison_id;
            } else {
                ?>
                <div>
                    <h2>Create a new Comparison</h2>
                    <div>
                        <label>Name your comparison: </label>
                        <input id="title" type="text">
                        
                        <div class="yui3-skin-sam">
                            <input id="ac-input" type="text" size="45">
                        </div>
                        <label>Vs</label>
                        <div class="yui3-skin-sam">
                            <input id="ac-input2" type="text" size="45">
                        </div>
                    </div>
                    <input type="button" class="btn btn-primary" value="Create" onClick="submitComparison()"/>
                </div>
                <script>
                    YUI().use('array-extras', 'autocomplete', 'autocomplete-highlighters', function (Y) {
                        function locateModules(response) {
                            var results = (response) || [];

                            return results;
                        }

                        Y.one('#ac-input').plug(Y.Plugin.AutoComplete, {
                            resultHighlighter: 'phraseMatch',
                            resultListLocator: locateModules,
                            resultTextLocator: function (result) {
                                Y.one('#ac-input').setAttribute('pid',result.id)
                                return result.name;                             
                            },
                            source: '/fbhackraks/dao/getItems.php?name={query}&callback={callback}&count=50'
                        });
                        Y.one('#ac-input2').plug(Y.Plugin.AutoComplete, {
                            resultHighlighter: 'phraseMatch',
                            resultListLocator: locateModules,
                            resultTextLocator: function (result) {
                                Y.one('#ac-input2').setAttribute('pid',result.id)
                                return result.name;                             
                            },
                            source: '/fbhackraks/dao/getItems.php?name={query}&callback={callback}&count=50'
                        });
                        
                    }); 
                    function submitComparison(){
                                        
                        YUI().use('io', function (Y){
                            var pid1 = Y.one('#ac-input').getAttribute('pid');
                            var pid2 = Y.one('#ac-input2').getAttribute('pid');
                            var ids = pid1 + "," + pid2;
                            var title = Y.one('#title').get('value');
                            var cfg = {
                                method: 'GET',
                                headers: {
                                    'Content-Type': 'application/json',
                                },
                                on: {                            
                                    complete: function(id, res){
                                        alert(res.responseText);
                                    }                                      
                                }
                            };
                            var uri = "/fbhackraks/dao/postComparision.php?ids="+ids
                            Y.io(uri, cfg);
                        });
                    }
                </script>
                <?php
            }
            ?>
        </div>
    </body>
</html>
